var WxParse = require('../../wxParse/wxParse.js');
//获取应用实例
const app = getApp()
Page({
	data: {
		imgpath:app.setting.apiimgpath,
		isshow:'',
		isshowbtn:'',
		bgcolor:'',
		coupon_id:'',
		ticketDetail:'',
		isclick:'',
		con_finish:0,
		btnText:'',
		type_coup:false
	},
	backIndex:function(){
		wx.switchTab({
			url:'/pages/index/index'
		})
	},
	//事件处理函数	 
	submitTicket:function(){
		var that = this;
		//我的订单
		app.request('/api/Coupon/exchange', {couponid: that.data.coupon_id}, function(res){
			if(res.code == '0'){
				wx.showToast({
					title:res.msg,
					icon:'none'
				})
				that.setData({
					isclick:true,
					btnText:"已领取",
					con_finish:1
				})
			}
		});	
	},
	onLoad:function(option){
		var that = this;
		this.setData({
			isshow:option.type,
			isshowbtn:option.btn,
			coupon_id:option.coupon_id
		})
		//优惠券详情
		if(option.coupon_id){
			app.request('/api/Coupon/couponInfo', {couponid:option.coupon_id}, function(res){
				WxParse.wxParse('content', 'html', res.data.introduce, that, 5);
				if(res.code == 0){
					that.setData({
						ticketDetail:res.data
					})
					if(res.data.is_finish == 1){
						that.setData({
							btnText:"已领完",
							isclick:true
						})
					}
					else{
						if(res.data.is_receive == 1){
							that.setData({
								isclick:true,
								btnText:"已领取"
							})
						}else{
							that.setData({
								btnText:"领取"
							})
						}
					}
				}
			});	
		}
		//我的优惠券详情
		if(option.id){
			app.request('/api/Coupon/getCouponInfo', {coupondetailsid:option.id}, function(res){
				WxParse.wxParse('content', 'html', res.data.introduce, that, 5);
				if(res.code == 0) {
					that.setData({
						ticketDetail:res.data
					})
					if(res.data.is_use==0 && res.data.type==4){
						that.setData({
							type_coup:true
						})
					}
					if(res.data.is_use == 1){
						that.setData({
							bgcolor:"#fff"
						})
					}					
				}
			});
		}
	},
	hexiaoClick:function(){
		let that = this
		wx.showModal({
			title:'提示',
			content:'亲，请到店后，出示该优惠券并在营业员确认下点击操作，误点无效哦~~',
			confirmText:"确定",
			showCancel:true,
			success:function(re){
				if(re.confirm){
					app.request('/api/Coupon/cancelCoupon', {sn:that.data.ticketDetail.serial_number}, function(res){
						wx.showToast({
							title:res.msg,
							icon: 'none'
						})
						if(res.code == '0'){
							that.setData({
								type_coup:false,
							})  
						}
					});	
				}
			}
		});
	}
})